<?php
namespace modules\iChat\objects;

use modules\iChat\libs\FS;
use modules\iChat\iChatException;

class User
{
	//文件存储
	const FILE_USER   		= PATH_BASE . 'modules/iChat/data/base-user.log';
	const FILE_USER_ONLINE  = PATH_BASE . 'modules/iChat/data/base-user-online.log';
	const FILE_FRIEND 		= PATH_BASE . 'modules/iChat/data/base-friend.log';
	const FILE_GROUP  		= PATH_BASE . 'modules/iChat/data/base-group.log';

	//好友列表

	public static function getUserMessage($uid)
	{

	}

	public static function getGroupMessage($gid)
	{
		
	}

	public static function getChatList($uid)
	{
		//用户对话
		$user  = FS::searchMulti($uid, Session::FILE_DIALOGUE_USER);
		//群组对话
		$group = FS::searchMulti($uid, Session::FILE_DIALOGUE_GROUP);
		if (is_bool($user) || is_bool($group)) {
			iChatException::throw(iChatException::FILE_NOT_EXIST);
		}
		foreach ([$user, $group]  as &$one) {
			usort($one, function($a, $b) {
				return $a['time'] > $b['time'] ? -1 : 1;
			});	
		}
		$info = array_merge($user, $group);
		$list = [];
		foreach ($info as $one) {
			$fid = $one['fid'];
			if (!isset($list[$fid])) {
				$list[$fid] = [];
				$list[$fid]['uid']  = $fid;
				$list[$fid]['user'] = FS::search($fid, User::FILE_USER);
			}
			$list[$fid]['message'][] = $one;
		}
		return array_values($list);
	}

	public static function getFriendList($uid)
	{
		$friends = FS::search($uid, User::FILE_FRIEND);
		if (is_bool($friends)) {
			iChatException::throw(iChatException::FILE_NOT_EXIST);
		}
		$friendIds = array_column($friends, 'uid');
		$list = [];
		foreach ($friendIds as $one) {
			$list[] = FS::search($one, User::FILE_USER);
		}
		return $list;
	}

	public static function getGroupUser($gid) //获取组内成员ID列表
	{

	}

	public static function getGroupList($uid) //获取组ID列表
	{

	}


}